package com.example.demo.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.WaresManageDetail;
import com.example.demo.service.WaresManageDetailService;
import com.example.demo.utils.JsonResult;
import com.example.demo.utils.ResultUtil;
import com.example.demo.utils.TimeUtil;
import com.example.demo.utils.UserInfoUtil;
import com.example.demo.vo.WaresManageDetailExport;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 货物明细管理
 */
@RestController
@RequestMapping(value = "/WaresManageDetail")
public class WaresManageDetailController {
    @Autowired
    private WaresManageDetailService waresManageDetailService;

    /**
     * 分页查询
     * @param
     * @return
     */
    @RequestMapping(value = "/getWaresManageDetailPage",method = RequestMethod.GET)
    public JsonResult getWaresManageDetailPage(@RequestParam(value = "current") Integer current,
                                               @RequestParam(value = "size")Integer size){
        IPage<List<WaresManageDetailExport>> waresManageDetailPage = waresManageDetailService.getWaresManageDetailPage(new Page<>(current, size));
        return ResultUtil.success("查询成功",waresManageDetailPage);
    }

    /**
     * 添加
     * @param waresManageDetail
     * @return
     */
    @RequestMapping(value = "/addWaresManage",method = RequestMethod.POST)
    public JsonResult addWaresManageDetail(@RequestBody WaresManageDetail waresManageDetail){
        Boolean save = waresManageDetailService.addWaresManageDetail(waresManageDetail);

        if(save){
            return ResultUtil.success("添加成功");
        }else{
            return ResultUtil.fail("添加失败");
        }
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(value = "/delWaresManageDetail/{id}",method = RequestMethod.DELETE)
    public JsonResult delWaresManageDetail(@PathVariable Integer id){
        boolean b = waresManageDetailService.removeById(id);
        if(b){
            return ResultUtil.success("删除成功");
        }else{
            return ResultUtil.fail("删除失败");
        }
    }

    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/editWaresManageDetail",method = RequestMethod.PUT)
    public JsonResult editWaresManageDetail(@RequestBody WaresManageDetail waresManageDetail){
        String userName = UserInfoUtil.gerUserName();
        String dateTime = TimeUtil.getDateTime();

        waresManageDetail.setEditName(userName);
        waresManageDetail.setUpdateDate(dateTime);

        boolean b = waresManageDetailService.updateById(waresManageDetail);
        if(b){
            return ResultUtil.success("修改成功");
        }else{
            return ResultUtil.fail("修改成功");
        }
    }

    /**
     * 报表
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value = "/getWaresManageDetailReport",method = RequestMethod.GET)
    public JsonResult getWaresManageDetailReport(@RequestParam(value = "startTime") String startTime,
                                                 @RequestParam(value = "endTime") String endTime){
        List<Map<String, String>> waresManageDetailReport = waresManageDetailService.getWaresManageDetailReport(startTime, endTime);
        return ResultUtil.success("查询成功",waresManageDetailReport);
    }

    @RequestMapping("/export")
    public void exportExcel(HttpServletResponse response,
                            @RequestParam(value = "current",defaultValue = "1") Integer current,
                            @RequestParam(value = "size",defaultValue = "10")Integer size) throws Exception {
        IPage<List<WaresManageDetailExport>> waresManageDetailPage = waresManageDetailService.getWaresManageDetailPage(new Page<>(current,size));
        List<List<WaresManageDetailExport>> records = waresManageDetailPage.getRecords();

        System.err.println(records);

        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("货物明细信息", "货物明细"), WaresManageDetailExport.class,records);
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("货物明细.xls","UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
        workbook.close();
    }
}
